package visaoInterface;

import dao.DaoVeiculo;
import java.util.Vector;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
import modelo.dominio.Veiculo;

public class VeiculosMaisProcurados extends javax.swing.JDialog {
    
    // Define o modelo da tabela e desabilita a edição da mesma.
    DefaultTableModel tmVeiculosMaisProcurados = new DefaultTableModel(null, new String[]{"Numero de Locações","Modelo","Marca","Placa","Cor"} ) {
        @Override
        public boolean isCellEditable(int row, int col)
        {
            return false;
        } 
    };
    
    
  
    /** Creates new form VeiculosMaisProcurados */
    public VeiculosMaisProcurados(java.awt.Dialog parent, boolean modal) {
        super(parent, modal);
        initComponents();
    }
    
    /**
     * Método que mostra na tabela os veiculos mais procurados de forma
     * ordenada.
     * @param veiculos - Recebe o vetor de veiculos do banco de dados
     * a partir do daoVeiculo.
     * @see daoVeiculo 
     */
    public void mostraVeiculosMaisProcurados(Vector<Veiculo> veiculos){
        String[] linha = new String[] {null,null,null,null,null};
        while(tmVeiculosMaisProcurados.getRowCount()>0){
            tmVeiculosMaisProcurados.removeRow(0);
        }
        for (int i=0 ; i<veiculos.size() ; i++) {
         tmVeiculosMaisProcurados.addRow(linha);
         tmVeiculosMaisProcurados.setValueAt(veiculos.get(i).getNumeroLocacoes(),i,0);
         tmVeiculosMaisProcurados.setValueAt(veiculos.get(i).getModelo(),i,1);
         tmVeiculosMaisProcurados.setValueAt(veiculos.get(i).getMarca(),i,2);
         tmVeiculosMaisProcurados.setValueAt(veiculos.get(i).getPlaca(),i,3);
         tmVeiculosMaisProcurados.setValueAt(veiculos.get(i).getCor(),i,4);
        }          
            
        if (tmVeiculosMaisProcurados.getRowCount() == 0){
            JOptionPane.showMessageDialog(null, "Registro nao encontrados.");
        }
    }
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Veiculos mais procurados"));
        jPanel1.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        jTable1.setModel(tmVeiculosMaisProcurados);
        jTable1.addPropertyChangeListener(new java.beans.PropertyChangeListener() {
            public void propertyChange(java.beans.PropertyChangeEvent evt) {
                jTable1PropertyChange(evt);
            }
        });
        jScrollPane1.setViewportView(jTable1);

        jPanel1.add(jScrollPane1, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 20, 700, 170));

        getContentPane().add(jPanel1, java.awt.BorderLayout.CENTER);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jTable1PropertyChange(java.beans.PropertyChangeEvent evt) {//GEN-FIRST:event_jTable1PropertyChange
        DaoVeiculo dv = new DaoVeiculo();
        this.mostraVeiculosMaisProcurados(dv.buscarMaisProcurados());
    }//GEN-LAST:event_jTable1PropertyChange

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(VeiculosMaisProcurados.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(VeiculosMaisProcurados.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(VeiculosMaisProcurados.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(VeiculosMaisProcurados.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the dialog */
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                VeiculosMaisProcurados dialog = new VeiculosMaisProcurados(new javax.swing.JDialog(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {

                    @Override
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTable1;
    // End of variables declaration//GEN-END:variables
}
